package com.gugee.tiktok.data.consumer.music.core.mapper;

import com.gugee.tiktok.common.model.mysql.Music;
import com.gugee.tiktok.common.model.spider.music.MusicInfo;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.springframework.util.CollectionUtils;

@Mapper(componentModel = "spring")
public interface MusicMapper {

    @Mapping(target = "userCount", source = "user_count")
    @Mapping(target = "original", source = "is_original")
    @Mapping(target = "offlineDesc", source = "offline_desc")
    @Mapping(target = "authorDeleted", source = "author_deleted")
    @Mapping(target = "playUrl", expression = "java(getPlayUrl(musicInfo))")
    @Mapping(target = "shareUrl", expression = "java(getShareUrl(musicInfo))")
    @Mapping(target = "uid", expression = "java(getUid(musicInfo))")
    @Mapping(target = "coverMedium", expression = "java(getCoverMedium(musicInfo))")
    Music mapper(MusicInfo musicInfo);


    default String getPlayUrl(MusicInfo musicInfo){
        if (CollectionUtils.isEmpty(musicInfo.getPlay_url().getUrl_list())) return "";
        return musicInfo.getPlay_url().getUrl_list().get(0);
    }

    default String getCoverMedium(MusicInfo musicInfo){
        if (CollectionUtils.isEmpty(musicInfo.getCover_medium().getUrl_list())) return "";
        return musicInfo.getCover_medium().getUrl_list().get(0);
    }

    default String getShareUrl(MusicInfo musicInfo){
        return musicInfo.getShare_info().getShare_url();
    }

    default Long getUid(MusicInfo musicInfo){
        if (CollectionUtils.isEmpty(musicInfo.getArtists())){
            if(null != musicInfo.getOwner_id()){
                return musicInfo.getOwner_id();
            }
            return 0l;
        };
        return musicInfo.getArtists().get(0).getUid();
    }
}
